package com.situ.struts.action;

import java.io.File;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.situ.struts.entity.Admin;

public class FileUploadAction extends ActionSupport 
	implements ModelDriven<Admin>{
	
	private Admin admin = new Admin();
	
	//<input type="file" name="fileUpload">
	private File fileUpload;
	//文件名：要求后面必须加上FileName
	private String fileUploadFileName;
	//文件类型：要求后面必须加上ContentType
	private String fileUploadContentType;

	@Override
	public Admin getModel() {
		return admin;
	}
	
	@Override
	public String execute() throws Exception {
		System.out.println("FileUploadAction.execute()");
		System.out.println(admin);
		//文件上传部分
		String path = ServletActionContext.getServletContext().getRealPath("/upload");
		//在upload文件夹下面创建一个名字叫fileUploadFileName文件
		File file = new File(path, fileUploadFileName);
		//将上传的问价拷贝到目标文件中
		FileUtils.copyFile(fileUpload, file);
		
		return SUCCESS;
	}

	public File getFileUpload() {
		return fileUpload;
	}

	public void setFileUpload(File fileUpload) {
		this.fileUpload = fileUpload;
	}

	public String getFileUploadFileName() {
		return fileUploadFileName;
	}

	public void setFileUploadFileName(String fileUploadFileName) {
		this.fileUploadFileName = fileUploadFileName;
	}

	public String getFileUploadContentType() {
		return fileUploadContentType;
	}

	public void setFileUploadContentType(String fileUploadContentType) {
		this.fileUploadContentType = fileUploadContentType;
	}

}
